const puppeteer = require('puppeteer');
const fs = require('fs');
const consola = require('consola')
const bookUrl = 'https://www.biquge.info/9_9414/'
let bookName
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(bookUrl, {waitUntil: 'networkidle2'});
  await consola.start('打开小说首页')
  // 获取小说名称
  bookName = await page.$eval('#maininfo>#info>h1', el => el.innerHTML) + '.txt'
  // 创建小说txt文件
  fs.writeFile(bookName, "", function(err) {
    if(err) {
        return console.log(err);
    }
    consola.success(`创建${bookName}成功，爬取内容...`)
  });
  // 获取小说全部章节
  const chapters = await page.$$eval('.box_con>#list>dl>dd>a', els => Array.from(els).map(el=> {
    return {
      title: el.innerHTML,
      url: el.href
    }
  }))
  await (async () => {
    for (let i = 0; i < chapters.length; i++) {
      // // 创建断点重连json文件
      // fs.writeFile('error.json', i, function(err) {
      //   if(err) {
      //       return console.log(err);
      //   }
      //   consola.success(`断点重连文件更新成功！！！`)
      // });
      await pro(chapters[i], page)
    }
  })()
})();

// 爬取内容
function pro(el, page){
  return new Promise(resolve => {
    (async () => {
      await page.goto(el.url, {waitUntil: 'networkidle2'});
      await consola.success(`打开${el.title}`)
      let content = await page.$eval('#content', el => el.innerHTML.replace(/&nbsp;/g, " ").replace(/(<br\/>|<br>|<BR>|<BR\/>)/g,"\n"))
      content = `    ${el.title}\n${content}\n`
      await content && consola.success(`${el.title}内容爬取成功`)
      await fs.appendFile(bookName, content, function(err) {
        if(err) {
            return console.log(err);
        }
        consola.success(`${el.title}内容写入成功`);
      });
      await setTimeout(() => {resolve()})
    })()
  })
}